package com.njuse.jvmfinal.Instruction;

import java.nio.ByteBuffer;

import com.njuse.jvmfinal.memory.jclass.JClass;
import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant;
import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.ref.ClassRef;
import com.njuse.jvmfinal.runtimestructure.Jframe;
import com.njuse.jvmfinal.runtimestructure.Jobject;

public class NEW extends Instruction {
	private int index;
	
	public  void readindex(ByteBuffer reader) {
		this.index=(int)reader.getShort();
	}
	
	public void execute(Jframe frame) {
		Constant classref=frame.getMymethod().getClazz().getRuntimeConstantPool().getConstant(index);
		ClassRef clazzref=(ClassRef) classref;
		JClass targetclass=clazzref.getclass();
		if (targetclass.isInitok()==false) {
			frame.setPc(frame.getPc()-3);
			targetclass.init(frame.getMythread());
			return;
		}
		Object temp=new Jobject(targetclass);
		Object[] stack=frame.getManagestack();
		int top=frame.getStacktop();
		stack[top]=temp;
		top++;
		frame.setManagestack(stack);
		frame.setStacktop(top);
		
	}
}
